﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using KxzzEntity.FriendsEntity;
using Kaixin001.Images;
using Kaixin001.Friend;
using Kaixin001.XmlUtil;

namespace Kxzz.Controls
{
    public partial class UserInfo : UserControl
    {
        public UserInfo()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化值
        /// </summary>
        /// <param name="Uid"></param>
        public void InitValue(string Uid)
        {
            try
            {
                this.Tag = Uid;
                FriendEntity fe = Init.AllFriendsDetail[Uid];
                string userInfoFileName = ".\\" + Init.UserDataFolder + "\\" + fe.Uid + "\\UserInfo.xml";
                string userLargeHead = ".\\" + Init.UserDataFolder + "\\" + fe.Uid + "\\" + Init.UserLargeUserHeadFileName;

                if (System.IO.File.Exists(userInfoFileName))
                {
                    FriendEntity friend = XmlHelper.XmlDeserialize(userInfoFileName, typeof(FriendEntity)) as FriendEntity;
                    if (friend != null && !string.IsNullOrEmpty(friend.RealName))
                    {
                        fe = friend;
                    }
                    else
                    {
                        FriendHelper friendHelper = new FriendHelper(Init.Kaixin001Helper.HttpSetting, Init.Kaixin001Helper.Cookies);
                        fe = friendHelper.GetFirendDetailByUID(Uid);
                        Init.AllFriendsDetail[Uid] = fe;
                        XmlHelper.XmlSerialize(userInfoFileName, fe, typeof(FriendEntity));
                    }
                }
                else
                {
                    FriendHelper friendHelper = new FriendHelper(Init.Kaixin001Helper.HttpSetting, Init.Kaixin001Helper.Cookies);
                    fe = friendHelper.GetFirendDetailByUID(Uid);
                    Init.AllFriendsDetail[Uid] = fe;
                    XmlHelper.XmlSerialize(userInfoFileName, fe, typeof(FriendEntity));
                }

                if (!string.IsNullOrEmpty(fe.RealName))
                {
                    this.labUserID.Text = fe.Uid;
                    this.labUserName.Text = fe.RealName;
                    this.labSex.Text = fe.Sex == 0 ? "男" : "女";

                    ImagesHelper imagesHelper = new ImagesHelper();
                    this.pbUserPhoto.Size = new Size(50, 50);

                    if (!System.IO.File.Exists(userLargeHead))
                    {
                        ImagesHelper ih = new ImagesHelper();
                        ih.SaveImage(fe.Icon, userLargeHead);
                    }

                    this.pbUserPhoto.Image = Image.FromFile(userLargeHead);

                    this.labBirthDay.Text = fe.Birthday.Replace("月", "/").Replace("年", "/").Replace("日", "");
                    this.labXz.Text = fe.Constellation;
                    this.labHomeTown.Text = fe.HomeTown;
                    this.labNowCity.Text = fe.LiveCity;
                    this.labSize.Text = fe.TheSize;
                    this.labeBlood.Text = fe.Blood;
                    this.labRelationShip.Text = fe.RelationShip;
                    this.linkLabelUpdate.Tag = Uid;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void UpdateFirend(object Uid)
        {
            if (Uid != null)
            {
                string userInfoFileName = ".\\" + Init.UserDataFolder + "\\" + Uid.ToString() + "\\UserInfo.xml";
                if (System.IO.File.Exists(userInfoFileName))
                {
                    System.IO.File.Delete(userInfoFileName);
                }
                this.InitValue((string)Uid);
            }
        }

        private void linkLabelUpdate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (this.Tag != null)
                {
                    System.Threading.ThreadPool.QueueUserWorkItem(this.UpdateFirend, this.Tag);
                }
            }
            catch (Exception ex)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show(
                        ex.Message,
                        "错误",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
            }
        }
    }
}
